{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0df0d850-49eb-4a0b-a27a-146969db710d",
   "metadata": {},
   "source": [
    "# The Price is Right\n",
    "\n",
    "## Week 8 Order of Play\n",
    "\n",
    "Day 1: Modal.com and SpecialistAgent  \n",
    "Day 2: RAG, FrontierAgent, Ensemble Agent  \n",
    "Day 3: ScannerAgent, MessengerAgent  \n",
    "Day 4: AutonomousPlannerAgent and DealAgentFramework  \n",
    "Day 5: The Price Is Right Finale\n",
    "\n",
    "\n",
    "Today we'll build another piece of the puzzle: a ScanningAgent that looks for promising deals by subscribing to RSS feeds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3763a79-8a5a-4300-8de4-93e85475af10",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "from agents.deals import ScrapedDeal, DealSelection\n",
    "import logging\n",
    "import requests\n",
    "load_dotenv(override=True)\n",
    "openai = OpenAI()\n",
    "MODEL = 'gpt-5-mini'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afece9db-8cd4-46be-ac57-0b472e84da7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "deals = ScrapedDeal.fetch(show_progress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cd15c4d-eb44-4601-bf0c-f945c1d8e3ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(deals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4259f30a-6455-49ed-8863-2f9ddd4776cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "deals[10].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53984504",
   "metadata": {},
   "source": [
    "### We are going to ask GPT-5-mini to summarize deals and identify their price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8100e5ac-38f5-40c1-a712-08ae12c85038",
   "metadata": {},
   "outputs": [],
   "source": [
    "SYSTEM_PROMPT = \"\"\"You identify and summarize the 5 most detailed deals from a list, by selecting deals that have the most detailed, high quality description and the most clear price.\n",
    "Respond strictly in JSON with no explanation, using this format. You should provide the price as a number derived from the description. If the price of a deal isn't clear, do not include that deal in your response.\n",
    "Most important is that you respond with the 5 deals that have the most detailed product description with price. It's not important to mention the terms of the deal; most important is a thorough description of the product.\n",
    "Be careful with products that are described as \"$XXX off\" or \"reduced by $XXX\" - this isn't the actual price of the product. Only respond with products when you are highly confident about the price. \n",
    "\"\"\"\n",
    "\n",
    "USER_PROMPT_PREFIX = \"\"\"Respond with the most promising 5 deals from this list, selecting those which have the most detailed, high quality product description and a clear price that is greater than 0.\n",
    "You should rephrase the description to be a summary of the product itself, not the terms of the deal.\n",
    "Remember to respond with a short paragraph of text in the product_description field for each of the 5 items that you select.\n",
    "Be careful with products that are described as \"$XXX off\" or \"reduced by $XXX\" - this isn't the actual price of the product. Only respond with products when you are highly confident about the price. \n",
    "\n",
    "Deals:\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "USER_PROMPT_SUFFIX = \"\\n\\nInclude exactly 5 deals, no more.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4bca170-af71-40c9-9597-1d72980c74d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# this makes a suitable user prompt given scraped deals\n",
    "\n",
    "def make_user_prompt(scraped):\n",
    "    user_prompt = USER_PROMPT_PREFIX\n",
    "    user_prompt += '\\n\\n'.join([scrape.describe() for scrape in scraped])\n",
    "    user_prompt += USER_PROMPT_SUFFIX\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "020947a6-561b-417b-98a0-a085e31d2ce3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's create a user prompt for the deals we just scraped, and look at how it begins\n",
    "\n",
    "user_prompt = make_user_prompt(deals)\n",
    "print(user_prompt[:2000])\n",
    "messages = [{\"role\": \"system\", \"content\": SYSTEM_PROMPT}, {\"role\": \"user\", \"content\": user_prompt}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7de46f74-868c-4127-8a68-cf2da7d600bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = openai.chat.completions.parse(model=MODEL, messages=messages, response_format=DealSelection, reasoning_effort=\"minimal\")\n",
    "results = response.choices[0].message.parsed\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "065cfe7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "for deal in results.deals:\n",
    "    print(deal.product_description)\n",
    "    print(deal.price)\n",
    "    print(deal.url)\n",
    "    print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e04f2b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "root = logging.getLogger()\n",
    "root.setLevel(logging.INFO)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bdc57fb-7497-47af-a643-6ba5a21cc17e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from agents.scanner_agent import ScannerAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "132278bc-217a-43a6-b6c4-724140c6a225",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = ScannerAgent()\n",
    "result = agent.scan()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e1d013a-c930-4dad-901b-41433379e14b",
   "metadata": {},
   "outputs": [],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ee2e837-1f1d-42d4-8bc4-51cccc343006",
   "metadata": {},
   "source": [
    "### Introducing Pushover\n",
    "\n",
    "Pushover is a nifty tool for sending Push Notifications to your phone.\n",
    "\n",
    "It's super easy to set up and install!\n",
    "\n",
    "Simply visit https://pushover.net/ and click 'Login or Signup' on the top right to sign up for a free account, and create your API keys.\n",
    "\n",
    "Once you've signed up, on the home screen, click \"Create an Application/API Token\", and give it any name (like AIEngineer) and click Create Application.\n",
    "\n",
    "Then add 2 lines to your `.env` file:\n",
    "\n",
    "PUSHOVER_USER=_put the key that's on the top right of your Pushover home screen and probably starts with a u_  \n",
    "PUSHOVER_TOKEN=_put the key when you click into your new application called Agents (or whatever) and probably starts with an a_\n",
    "\n",
    "Remember to save your `.env` file, and run `load_dotenv(override=True)` after saving, to set your environment variables.\n",
    "\n",
    "Finally, click \"Add Phone, Tablet or Desktop\" to install on your phone."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "761bc7d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7fafd66",
   "metadata": {},
   "outputs": [],
   "source": [
    "pushover_user = os.getenv('PUSHOVER_USER')\n",
    "pushover_token = os.getenv('PUSHOVER_TOKEN')\n",
    "pushover_url = \"https://api.pushover.net/1/messages.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d531ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "if pushover_user:\n",
    "    print(f\"Pushover user found and starts with {pushover_user[0]}\")\n",
    "else:\n",
    "    print(\"Pushover user not found\")\n",
    "\n",
    "if pushover_token:\n",
    "    print(f\"Pushover token found and starts with {pushover_token[0]}\")\n",
    "else:\n",
    "    print(\"Pushover token not found\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "003c858f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def push(message):\n",
    "    print(f\"Push: {message}\")\n",
    "    payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
    "    requests.post(pushover_url, data=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43fd1fa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "push(\"MASSIVE DEAL!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be0c3276",
   "metadata": {},
   "outputs": [],
   "source": [
    "from agents.messaging_agent import MessagingAgent\n",
    "\n",
    "agent = MessagingAgent()\n",
    "agent.push(\"SUCH A MASSIVE DEAL!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1200929",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.notify(\"A special deal on Sumsung 60 inch LED TV going at a great bargain\", 300, 1000, \"www.samsung.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46d59b1e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
